package cn.tedu.boot302.controller;

import cn.tedu.boot302.mapper.ItemMapper;
import cn.tedu.boot302.vo.Item;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {
    @Autowired(required = false)
    ItemMapper mapper;

    @RequestMapping("/insert")
    public String insert(Item item){
        mapper.insert(item);
        return "增加完了";
    }
    @RequestMapping("/select")
    public String string(){
        List<Item>list=mapper.select();
        String html = "<table border=1>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>库存</th><th>价格</th><th>介绍</th><th>操作</th></tr>";
        //添加多行商品信息
        for (Item item:list) {
            html+="<tr>";
            html+="<td>"+item.getId()+"</td>";
            html+="<td>"+item.getName()+"</td>";
            html+="<td>"+item.getNum()+"</td>";
            html+="<td>"+item.getPrice()+"</td>";
            html+="<td>"+item.getIntro()+"</td>";
            html+="<td><a href='/delete?id="+item.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删完了";
    }
    @RequestMapping("update")
    public String update(Item item){
        mapper.update(item);
        return "改完了";
    }
}
